/**
 * @Title: ExampleFile.java
 * @Package com.example.package
 * @Description: Placeholder (briefly describe the purpose of this file)
 * @Author: Evan
 * @Date: 2023-08-18
 * @Version: V1.0
 *
 * Copyright Notice:
 * This project is released in open source and follows the applicable open source license.
 * All rights reserved. (c) 2023 Evan.
 * This project is protected by copyright laws and international copyright treaties.
 * Unauthorized reproduction, sale, copy, modification, merge, publication, distribution,
 * licensing, and/or sales of this software and its copies are prohibited without permission.
 * Please contact the author for authorization.
 * Contact Information:
 * @Email: uyevan@163.com
 */

package com.evan.biznak.vip.Util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.evan.biznak.vip.Config.Config;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;


public class ShaUtil {
    /*比较签名*/
    public static boolean isOrgApp(Context context) throws PackageManager.NameNotFoundException, NoSuchAlgorithmException {
        String current_sha1 = getAppSha1(context);
        current_sha1 = current_sha1.replace(":", "");
        // Log.e("CONFIG解密后的SHA", FileUtil.evanVip(Config.SHA, context));
        return FileUtil.evanVip(Config.SHA, context).equals(current_sha1);
    }

    /*生成sha1的签名*/
    public static String getAppSha1(Context context) throws PackageManager.NameNotFoundException, NoSuchAlgorithmException {
        @SuppressLint("PackageManagerGetSignatures") PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
        byte[] cert = info.signatures[0].toByteArray();
        MessageDigest md = MessageDigest.getInstance("SHA1");
        byte[] publicKey = md.digest(cert);
        StringBuilder hexString = new StringBuilder();
        for (byte b : publicKey) {
            String appendString = Integer.toHexString(0xFF & b).toUpperCase(Locale.US);
            if (appendString.length() == 1) {
                hexString.append("0");
            }
            hexString.append(appendString);
            hexString.append(":");
        }
        String result = hexString.toString();
        return result.substring(0, result.length() - 1);
    }

    /* 获取SHA值 */
    public static String getPages(Context context) throws PackageManager.NameNotFoundException, NoSuchAlgorithmException {
        String current_sha1 = getAppSha1(context);
        current_sha1 = current_sha1.replace(":", "");
        return current_sha1.substring(0, 16);
    }
}
